//
//  LCYRadioDetailController.m
//  EPA
//
//  Created by lanou3g on 16/4/21.
//  Copyright © 2016年 liu. All rights reserved.
//

#import "LCYRadioDetailController.h"
#import "LCYNetWorkRequestManager.h"
#import "LCYRadioDetailCell.h"
#import "LCYRadioDetailModel.h"
#import "LCYRadioPlayViewController.h"
#import "LCYDownloadTableController.h"
#import "LCYDownloadManager.h"
#import "LCYUserInfoManager.h"
#import "LCYLoginViewController.h"

@interface LCYRadioDetailController ()

/// 数据源数组
@property (nonatomic,strong) NSMutableArray *radioDetailListArray;
@property (nonatomic,strong) NSString *start;


@end

@implementation LCYRadioDetailController

- (void)viewDidLoad {
    [super viewDidLoad];

    
    self.title = self.navTitle;
    /// 初始化
    self.radioDetailListArray = [NSMutableArray array];
    
    // 网络请求
    [self loadDataFromServerRadioID:self.radioid];
    
    // 注册
    [self.tableView registerNib:[UINib nibWithNibName:@"LCYRadioDetailCell" bundle:nil] forCellReuseIdentifier:@"radioDetail"];
    self.tableView.separatorStyle=UITableViewCellSeparatorStyleSingleLine;
    
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"下载" style:UIBarButtonItemStylePlain target:self action:@selector(didClickDownloadBtn:)];
    
}
- (void)didClickDownloadBtn:(UIBarButtonItem *)sender {
    ///跳转到下载列表页面
    LCYDownloadTableController *downloadTC = [[LCYDownloadTableController alloc]init];
    [self.navigationController pushViewController:downloadTC animated:YES];
}


/// 网络请求
- (void)loadDataFromServerRadioID:(NSString *)radioid {
    [LCYNetWorkRequestManager requestURL:[NSURL URLWithString:RADIODETAILLIST_URL] RequestMethod:LCY_POST Params:@{@"auth":@"",@"client":@"2",@"deviceid":@"63A94D37-33F9-40FF-9EBB-481182338873",@"radioid":self.radioid,@"version":@"3.0.4"} Result:^(NSData *data, NSURLResponse *response, NSError *error) {
        NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
        if (data) {
            NSMutableDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
            NSDictionary *daraDic = jsonDic[@"data"];
            for (NSDictionary *itemDic in daraDic[@"list"]) {
                LCYRadioDetailModel *detailModel = [[LCYRadioDetailModel alloc]init];
                [detailModel setValuesForKeysWithDictionary:itemDic];
                [self.radioDetailListArray addObject:detailModel];
            }
            NSLog(@"%ld",self.radioDetailListArray.count);
            dispatch_async(dispatch_get_main_queue(), ^{
                [self.tableView reloadData];
            });
        }
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
#warning Incomplete implementation, return the number of sections
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
#warning Incomplete implementation, return the number of rows
    return self.radioDetailListArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    LCYRadioDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:@"radioDetail" forIndexPath:indexPath];
    
    // Configure the cell...
    cell.detailModel = self.radioDetailListArray[indexPath.row];
    
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 120;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if ([LCYUserInfoManager shareManager].uid) {
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"下载 or 播放音频" preferredStyle:UIAlertControllerStyleAlert];
        [alertC addAction:[UIAlertAction actionWithTitle:@"下载" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            LCYRadioDetailModel *model = self.radioDetailListArray[indexPath.row];
            [[LCYDownloadManager shareDownload]addDownloadTask:model Progress:nil Complate:nil];
        }]];
        [alertC addAction:[UIAlertAction actionWithTitle:@"播放" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            // 跳转到音频播放页面
            LCYRadioPlayViewController *playTVC = [[LCYRadioPlayViewController alloc]init];
            playTVC.radioArray = self.radioDetailListArray;
            playTVC.currentIndex = indexPath.row;
            
            [self.navigationController pushViewController:playTVC animated:YES];
        }]];
        [alertC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }]];
        
        
        [self presentViewController:alertC animated:YES completion:^{
            
        }];

    } else {
        UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"提示" message:@"请先登录" preferredStyle:UIAlertControllerStyleAlert];
        [alertC addAction:[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self login];
        }]];
        
        [self presentViewController:alertC animated:YES completion:^{
            
        }];
        
    }
}

- (void)login {
    LCYLoginViewController *loginVC = [[LCYLoginViewController alloc]init];
    UINavigationController *navc = [[UINavigationController alloc]initWithRootViewController:loginVC];
    [self presentViewController:navc animated:YES completion:nil];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
